package ch.dreamweb.readpractice.client.lesetraining.train.view;

import ch.dreamweb.readpractice.client.lesetraining.widget.NineButtonWidget;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiFactory;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class Unit2ViewImpl extends UnitViewImpl implements Unit2View {

	private static Unit2ViewUiBinder uiBinder = GWT
			.create(Unit2ViewUiBinder.class);

	@UiTemplate("Unit2View.ui.xml")
	interface Unit2ViewUiBinder extends UiBinder<Widget, Unit2ViewImpl> {
	}

	@UiField
	CssUnitStyle style;

	@UiField
	Button buttonStart;
	@UiField
	TextBox textBoxMorphem;

	@UiField
	Button buttonOk;

	@UiField
	Label labelInfo;

	@UiField
	NineButtonWidget nine;

	@UiField
	HTML labelInfoNine;

	public Unit2ViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));

		init();
	}

	@Override
	public void init() {
		super.init();

		// Can access @UiField after calling createAndBindUi
		buttonOk.setVisible(false);
		textBoxMorphem.setVisible(true);
		textBoxMorphem.setText("");
		textBoxMorphem.setText("");
		textBoxMorphem.setMaxLength(8);
		textBoxMorphem.setVisibleLength(8);
		textBoxMorphem.setReadOnly(true);
		textBoxMorphem.setTabIndex(-1);
		buttonOk.setText("OK");
		buttonOk.setTabIndex(1);

		nine.setVisible(false);

		labelInfoNine.setText("");
	}

	// Add a UI Factory method for the sub-widget & pass object
	@UiFactory
	NineButtonWidget makeNineButtonWidget() {
		Log.debug("enter makeNineButtonWidget");
		return new NineButtonWidget(2);
	}

	@Override
	public void setPresenter(Presenter presenter) {
		super.setPresenter(presenter);
		nine.setEventBus(presenter.getEventBus());
	}

	@UiHandler("buttonStart")
	void onButtonStartClick(ClickEvent e) {
		if (presenter != null) {
			presenter.onStartButtonClicked();
		}
	}

	@UiHandler("buttonOk")
	void onButtonOkClick(ClickEvent e) {
		if (presenter != null) {
			presenter.onOkButtonClicked();
		}

	}

	@Override
	public Widget asWidget() {
		return this;
	}

	@Override
	public void setMorphem(String morphem) {
		textBoxMorphem.setText(morphem);
	}

	@Override
	public void setOkEnabled(boolean enabled) {
		buttonOk.setEnabled(enabled);
		buttonOk.setFocus(true);
	}

	@Override
	public void setStartEnabled(boolean enabled) {
		buttonStart.setEnabled(enabled);
	}

	@Override
	public void setFocusStart() {
		buttonStart.setFocus(true);
	}

	@Override
	public void setMorphem20(String morphem20) {
	}

	@Override
	public void setNine(String[] all, int buttonNumber) {
		nine.setAllTexts(all, buttonNumber);
		nine.setEnabled(true);
		nine.setVisible(true);
		nine.setFocus();
	}

	@Override
	public void setNineVisible(boolean visible) {
		nine.setVisible(visible);
	}

	@Override
	public void setNineEnabled(boolean enabled) {
		nine.setEnabled(enabled);
	}

	@Override
	public void setInfoNine(String text) {
		labelInfoNine.setText(text);
	}

	@Override
	public void addInfoNineCorrect(String correct) {
		labelInfoNine.setHTML(labelInfoNine.getHTML() + "&nbsp;<span class=\""
				+ style.correct() + "\">" + correct + "</span>");
	}

	@Override
	public void addInfoNineIncorrect(String wrong, String correct) {
		labelInfoNine.setHTML(labelInfoNine.getHTML() + "&nbsp;<span class=\""
				+ style.incorrect() + " " + style.stroke() + "\">" + wrong
				+ "</span>&nbsp;<span class=\"" + style.correct() + "\">"
				+ correct + "</span>");
	}

	@Override
	public Label getLabelInfo() {
		return labelInfo;
	}

	@Override
	public CssUnitStyle getStyle() {
		return style;
	}

	@Override
	public Button getButtonStart() {
		return buttonStart;
	}

}
